package com.api.user.controller;

import com.api.common.EmailUtil;
import com.api.core.annotation.PowerEnable;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.GameProfile;
import com.api.user.model.Team;
import com.api.user.model.TeamUser;
import com.api.user.model.UserInfo;
import com.api.user.service.GameProfileService;
import com.api.user.service.TeamService;
import com.api.user.service.TeamUserService;
import com.api.user.service.UserInfoService;
import io.swagger.annotations.*;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* Created by wanghuiwen on 2021/03/26.
*/
@PowerEnable(name = "战队",url = "/team")
@Api(value = "战队", tags = {"战队"})
@RestController
@RequestMapping("/team")
public class TeamController extends Ctrl{
    @Resource
    private TeamService teamService;

    @Resource
    private TeamUserService teamUserService;

    @Resource
    private GameProfileService gameProfileService;

    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "战队添加", tags = {"Team"}, notes = "战队添加")
    @PostMapping(value="/add",name="战队添加")
    public Result add(@ApiParam Team team, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        List<GameProfile> profiles = gameProfileService.findByUserId(getAuthUser(authentication).getId());
        if(profiles==null || profiles.size()<1){
            return  ResultGenerator.genFailResult(400,"first add game Profile");
        }
        team.setLeaderId(userId);
        team.setGameType(1);
        return teamService.add(team);
    }

    @ApiOperation(value = "邀请用户加入战队", tags = {"Team"}, notes = "邀请用户加入战队")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamId",required=true, value = "teamId", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userIds",required=true, value = "userIds", dataType = "String", paramType = "query")
    })
    @PostMapping(value="/invite",name="邀请用户加入战队")
    public Result invite(@RequestParam Long teamId,@RequestParam String userIds) {
        Result result = teamUserService.invite(teamId,userIds);
        if(result.getCode() == 200){
            String [] userids = userIds.split(",");
            for (String userid : userids) {
                UserInfo info = userInfoService.findById(userid);
                EmailUtil.gmailSender(info.getEmail(),"Your strength has been discovered by a team, accept the invitation and join the alliance !!",null);
            }
        }
        return result;
    }

    @ApiOperation(value = "移出战队", tags = {"Team"}, notes = "移出战队")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamId",required=true, value = "teamId", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userIds",required=true, value = "userIds", dataType = "String", paramType = "query")
    })
    @PostMapping(value="/move/out",name="移出战队")
    public Result moveOut(@RequestParam Long teamId,@RequestParam String userIds) {

        return teamUserService.moveOut(teamId,userIds);
    }

    @ApiOperation(value = "申请加入战队", tags = {"Team"}, notes = "申请加入战队")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamId",required=true, value = "teamId", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userId",required=true, value = "userId", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/join",name="申请加入战队")
    public Result join(@RequestParam Long teamId,@RequestParam Long userId) {
        Result result = teamUserService.join(teamId,userId);

        if(result.getCode() == 200){
            UserInfo info = userInfoService.findById(userId);
            EmailUtil.gmailSender(info.getEmail(),"A friend has requested to join your team !! Find out who and start your team's legacy now !!",null);
        }
        return result;
    }

    @ApiOperation(value = "同意/拒绝 加入队伍", tags = {"Team"}, notes = "同意/拒绝 加入队伍")
    @PostMapping(value="/decision",name="同意/拒绝 加入队伍")
    public Result decision(@ApiParam TeamUser teamUser,Authentication authentication) {
        List<GameProfile> profiles = gameProfileService.findByUserId(getAuthUser(authentication).getId());
        if(profiles==null || profiles.size()<1){
            return  ResultGenerator.genFailResult(400,"first add game Profile");
        }
        Result result =  teamUserService.decision(teamUser);
        if(result.getCode() == 200 && teamUser.getAgree() == 2){
            UserInfo info = userInfoService.findById(teamUser.getUserId());
            Team team = teamService.findById(teamUser.getTeamId());
            UserInfo leader = userInfoService.findById(team.getLeaderId());
            EmailUtil.gmailSender(leader.getEmail(),info.getFullName() + " has joined your team. Are you ready to show the world who you guys are ?!!",null);
        }
        return result;
    }

    @ApiOperation(value = "申请加入战队的人员", tags = {"Team"}, notes = "申请加入战队的人员")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "teamId", value = "战队id", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/applications", name = "申请加入战队的人员")
    public Result applications(@RequestParam(defaultValue = "") Long teamId,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        return teamUserService.applications(teamId, order, page, size);
    }

    @ApiOperation(value = "获取战队成员", tags = {"Team"}, notes = "获取战队成员")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "teamId", value = "战队id", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/users", name = "申请加入战队的人员")
    public Result users(@RequestParam(defaultValue = "") Long teamId,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        return teamUserService.users(teamId, order, page, size);
    }


    @ApiOperation(value = "战队列表", tags = {"Team"}, notes = "战队列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gameId", value = "游戏id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "teamName", value = "战队名字", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "战队列表")
    public Result list(@RequestParam(required = false) Long gameId,@RequestParam(required = false) String teamName,
                        @RequestParam(defaultValue = "{}") String order,
                        @RequestParam(defaultValue = "0") Integer page,
                        @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("gameId",gameId);
        params.put("teamName",teamName);

        return teamService.list(params, order, page, size);
    }

    @ApiOperation(value = "战队参加的赛事", tags = {"Team"}, notes = "战队参加的赛事")
    @PostMapping(value = "/tournaments", name = "战队参加的赛事")
    public Result tournaments(
            @RequestParam Long teamId,
            @RequestParam(required = false) Byte state,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("teamId",teamId);
        params.put("state",state);

        return teamService.tournaments(params, page, size);
    }

    @ApiOperation(value = "战队详细信息", tags = {"Team"}, notes = "战队列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamId", value = "战队id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/achievement", name = "战队详细信息")
    public Result achievement(Long teamId) {
        return teamService.achievement(teamId);
    }


    @PostMapping(value = "/user/list", name = "用户战队查询")
    public Result userList(String title,Integer type,@RequestParam(required = false) Long gameId,Authentication authentication) {
        Map<String, Object> params = new HashMap<>();
        params.put("title",title);
        params.put("type",type);
        params.put("gameId",gameId);
        return teamService.userList(params,getAuthUser(authentication));
    }

    @PostMapping(value = "/user/search", name = "用户查询")
    public Result userList(String name,Long teamId,Authentication authentication) {
        Map<String, Object> params = new HashMap<>();
        params.put("fullName",name);
        params.put("teamId",teamId);
        params.put("isFriend",true);
        params.put("isFriendId",getAuthUser(authentication).getId());

        return userInfoService.list(params);
    }

    @ApiOperation(value = "移交战队", tags = {"Team"}, notes = "移交战队")
    @PostMapping(value="/transfer",name="移交战队")
    public Result transfer(Long toUser, Long teamId, Authentication authentication) {

        return teamService.transfer(toUser,teamId,getAuthUser(authentication).getId());
    }


    @ApiOperation(value = "转换", tags = {"Team"}, notes = "转换")
    @PostMapping(value="/move",name="转换")
    public Result move(Long toUser, Long teamId, Long toTeam) {

        return teamService.moveto(teamId,toUser,toTeam);
    }
}
